<template>
  <div class="teacher-workload">
    <div class="page-header">
      <h2>工作量统计</h2>
    </div>

    <!-- 筛选条件 -->
    <el-card class="filter-card" shadow="never">
      <el-form :model="filterForm" :inline="true" class="filter-form">
        <el-form-item label="学期">
          <el-select
            v-model="filterForm.semester"
            placeholder="请选择学期"
            clearable
            style="width: 200px"
            @change="handleFilter"
          >
            <el-option label="2024春季" value="2024春季" />
            <el-option label="2024秋季" value="2024秋季" />
            <el-option label="2025春季" value="2025春季" />
            <el-option label="2025秋季" value="2025秋季" />
          </el-select>
        </el-form-item>
        
        <el-form-item>
          <el-button type="primary" @click="handleFilter">
            <el-icon><Search /></el-icon>
            查询
          </el-button>
          <el-button @click="handleReset">
            <el-icon><Refresh /></el-icon>
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 反馈列表 -->
    <el-card class="feedback-card" shadow="hover">
      <template #header>
        <div class="card-header">
          <span>反馈列表</span>
          <div class="header-actions">
            <el-button type="success" @click="handleAddFeedback">
              <el-icon><Plus /></el-icon>
              新增反馈
            </el-button>
          </div>
        </div>
      </template>
      
      <div class="feedback-content">
        <el-table
          :data="feedbackList"
          v-loading="loading"
          stripe
          border
          style="width: 100%"
        >
          <el-table-column prop="title" label="反馈标题" width="200" />
          <el-table-column prop="type" label="反馈类型" width="120">
            <template #default="scope">
              <el-tag :type="getTypeTagType(scope.row.type)">
                {{ getTypeText(scope.row.type) }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="content" label="反馈内容" width="300" show-overflow-tooltip />
          <el-table-column prop="status" label="状态" width="100">
            <template #default="scope">
              <el-tag :type="getStatusTagType(scope.row.status)">
                {{ getStatusText(scope.row.status) }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="createTime" label="提交时间" width="160" />
          <el-table-column prop="replyTime" label="回复时间" width="160" />
          <el-table-column label="操作" width="200" fixed="right">
            <template #default="scope">
              <el-button
                type="primary"
                size="small"
                @click="handleViewDetail(scope.row)"
              >
                查看详情
              </el-button>
              <el-button
                v-if="scope.row.status === 'pending'"
                type="warning"
                size="small"
                @click="handleEdit(scope.row)"
              >
                编辑
              </el-button>
              <el-button
                v-if="scope.row.status === 'pending'"
                type="danger"
                size="small"
                @click="handleDelete(scope.row)"
              >
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页 -->
        <div class="pagination-container">
          <el-pagination
            v-model:current-page="pagination.currentPage"
            v-model:page-size="pagination.pageSize"
            :page-sizes="[10, 20, 50, 100]"
            :total="pagination.total"
            layout="total, sizes, prev, pager, next, jumper"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
    </el-card>

    <!-- 新增/编辑反馈对话框 -->
    <el-dialog
      v-model="dialogVisible"
      :title="dialogTitle"
      width="600px"
      @close="handleDialogClose"
    >
      <el-form
        ref="formRef"
        :model="form"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="反馈标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入反馈标题" />
        </el-form-item>
        
        <el-form-item label="反馈类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择反馈类型" style="width: 100%">
            <el-option label="课程反馈" value="course" />
            <el-option label="教学反馈" value="teaching" />
            <el-option label="系统反馈" value="system" />
            <el-option label="其他反馈" value="other" />
          </el-select>
        </el-form-item>
        
        <el-form-item label="反馈内容" prop="content">
          <el-input 
            v-model="form.content" 
            type="textarea" 
            :rows="6"
            placeholder="请详细描述您的反馈内容" 
          />
        </el-form-item>
        
        <el-form-item label="联系方式" prop="contact">
          <el-input v-model="form.contact" placeholder="请输入联系方式（可选）" />
        </el-form-item>
      </el-form>
      
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSubmit">确定</el-button>
        </span>
      </template>
    </el-dialog>

    <!-- 反馈详情对话框 -->
    <el-dialog
      v-model="detailDialogVisible"
      title="反馈详情"
      width="700px"
    >
      <div class="feedback-detail">
        <el-descriptions :column="2" border>
          <el-descriptions-item label="反馈标题">
            {{ feedbackDetail.title }}
          </el-descriptions-item>
          <el-descriptions-item label="反馈类型">
            <el-tag :type="getTypeTagType(feedbackDetail.type)">
              {{ getTypeText(feedbackDetail.type) }}
            </el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="反馈状态">
            <el-tag :type="getStatusTagType(feedbackDetail.status)">
              {{ getStatusText(feedbackDetail.status) }}
            </el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="提交时间">
            {{ feedbackDetail.createTime }}
          </el-descriptions-item>
          <el-descriptions-item label="联系方式" :span="2">
            {{ feedbackDetail.contact || '未填写' }}
          </el-descriptions-item>
          <el-descriptions-item label="反馈内容" :span="2">
            <div class="content-text">{{ feedbackDetail.content }}</div>
          </el-descriptions-item>
        </el-descriptions>

        <!-- 回复信息 -->
        <div v-if="feedbackDetail.reply" class="reply-section">
          <h4>管理员回复</h4>
          <el-descriptions :column="2" border>
            <el-descriptions-item label="回复时间">
              {{ feedbackDetail.replyTime }}
            </el-descriptions-item>
            <el-descriptions-item label="回复人">
              {{ feedbackDetail.replyUser || '系统管理员' }}
            </el-descriptions-item>
            <el-descriptions-item label="回复内容" :span="2">
              <div class="content-text">{{ feedbackDetail.reply }}</div>
            </el-descriptions-item>
          </el-descriptions>
        </div>
      </div>
      
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="detailDialogVisible = false">关闭</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Search, Refresh, Plus } from '@element-plus/icons-vue'

// 筛选表单
const filterForm = reactive({
  type: '',
  status: '',
  dateRange: []
})

// 分页数据
const pagination = reactive({
  currentPage: 1,
  pageSize: 10,
  total: 0
})

// 加载状态
const loading = ref(false)

// 反馈列表
const feedbackList = ref([])

// 对话框相关
const dialogVisible = ref(false)
const dialogTitle = ref('')
const isEdit = ref(false)

// 详情对话框
const detailDialogVisible = ref(false)
const feedbackDetail = ref({})

// 表单数据
const form = reactive({
  id: null,
  title: '',
  type: '',
  content: '',
  contact: ''
})

// 表单验证规则
const rules = {
  title: [
    { required: true, message: '请输入反馈标题', trigger: 'blur' }
  ],
  type: [
    { required: true, message: '请选择反馈类型', trigger: 'change' }
  ],
  content: [
    { required: true, message: '请输入反馈内容', trigger: 'blur' }
  ]
}

const formRef = ref()

// 获取类型标签类型
const getTypeTagType = (type) => {
  const typeMap = {
    'course': 'primary',
    'teaching': 'success',
    'system': 'warning',
    'other': 'info'
  }
  return typeMap[type] || 'info'
}

// 获取类型文本
const getTypeText = (type) => {
  const typeMap = {
    'course': '课程反馈',
    'teaching': '教学反馈',
    'system': '系统反馈',
    'other': '其他反馈'
  }
  return typeMap[type] || '未知'
}

// 获取状态标签类型
const getStatusTagType = (status) => {
  const statusMap = {
    'pending': 'warning',
    'processed': 'success',
    'closed': 'info'
  }
  return statusMap[status] || 'info'
}

// 获取状态文本
const getStatusText = (status) => {
  const statusMap = {
    'pending': '待处理',
    'processed': '已处理',
    'closed': '已关闭'
  }
  return statusMap[status] || '未知'
}

// 获取反馈列表
const fetchFeedbackList = async () => {
  loading.value = true
  
  try {
    // 模拟数据，实际应该调用API
    const mockData = [
      {
        id: 1,
        title: '课程时间安排建议',
        type: 'course',
        content: '希望高等数学课程能够安排在上午，学生注意力更集中',
        status: 'processed',
        createTime: '2024-01-15 10:30:00',
        replyTime: '2024-01-16 14:20:00',
        contact: '13800138000',
        reply: '感谢您的建议，我们会考虑在下次排课时优先安排上午时段',
        replyUser: '教务处管理员'
      },
      {
        id: 2,
        title: '教室设备故障',
        type: 'system',
        content: 'A101教室的投影仪无法正常使用，影响教学效果',
        status: 'pending',
        createTime: '2024-01-20 09:15:00',
        replyTime: null,
        contact: 'teacher@example.com'
      },
      {
        id: 3,
        title: '学生出勤率低',
        type: 'teaching',
        content: '计算机1班学生出勤率较低，建议加强考勤管理',
        status: 'closed',
        createTime: '2024-01-10 16:45:00',
        replyTime: '2024-01-12 10:30:00',
        contact: '13800138001',
        reply: '已联系班主任加强学生管理，感谢反馈',
        replyUser: '学生处管理员'
      }
    ]
    
    feedbackList.value = mockData
    pagination.total = mockData.length
    
  } catch (error) {
    console.error('获取反馈列表失败:', error)
    ElMessage.error('获取反馈列表失败')
  } finally {
    loading.value = false
  }
}

// 筛选
const handleFilter = () => {
  pagination.currentPage = 1
  fetchFeedbackList()
}

// 重置筛选
const handleReset = () => {
  Object.keys(filterForm).forEach(key => {
    filterForm[key] = key === 'dateRange' ? [] : ''
  })
  pagination.currentPage = 1
  fetchFeedbackList()
}

// 新增反馈
const handleAddFeedback = () => {
  dialogTitle.value = '新增反馈'
  isEdit.value = false
  resetForm()
  dialogVisible.value = true
}

// 查看详情
const handleViewDetail = (row) => {
  feedbackDetail.value = { ...row }
  detailDialogVisible.value = true
}

// 编辑反馈
const handleEdit = (row) => {
  dialogTitle.value = '编辑反馈'
  isEdit.value = true
  Object.keys(form).forEach(key => {
    form[key] = row[key]
  })
  dialogVisible.value = true
}

// 删除反馈
const handleDelete = (row) => {
  ElMessageBox.confirm(
    `确定要删除反馈 "${row.title}" 吗？`,
    '删除确认',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }
  ).then(() => {
    // 模拟删除操作
    const index = feedbackList.value.findIndex(item => item.id === row.id)
    if (index > -1) {
      feedbackList.value.splice(index, 1)
      pagination.total--
      ElMessage.success('删除成功')
    }
  }).catch(() => {
    ElMessage.info('已取消删除')
  })
}

// 提交表单
const handleSubmit = async () => {
  const valid = await formRef.value.validate()
  if (valid) {
    try {
      if (isEdit.value) {
        // 编辑反馈
        const index = feedbackList.value.findIndex(item => item.id === form.id)
        if (index > -1) {
          feedbackList.value[index] = { ...feedbackList.value[index], ...form }
        }
        ElMessage.success('编辑成功')
      } else {
        // 新增反馈
        const newFeedback = {
          id: Date.now(),
          ...form,
          status: 'pending',
          createTime: new Date().toLocaleString(),
          replyTime: null
        }
        feedbackList.value.unshift(newFeedback)
        pagination.total++
        ElMessage.success('提交成功')
      }
      dialogVisible.value = false
    } catch (error) {
      console.error('操作失败:', error)
      ElMessage.error('操作失败，请重试')
    }
  }
}

// 关闭对话框
const handleDialogClose = () => {
  resetForm()
  formRef.value?.resetFields()
}

// 重置表单
const resetForm = () => {
  Object.keys(form).forEach(key => {
    form[key] = key === 'id' ? null : ''
  })
}

// 分页大小改变
const handleSizeChange = (val) => {
  pagination.pageSize = val
  pagination.currentPage = 1
  fetchFeedbackList()
}

// 当前页改变
const handleCurrentChange = (val) => {
  pagination.currentPage = val
  fetchFeedbackList()
}

// 组件挂载时获取数据
onMounted(() => {
  fetchFeedbackList()
})
</script>

<style scoped>
.teacher-feedback {
  background: #f5f7fa;
  padding: 20px;
  font-family: 'PingFang SC', 'Microsoft YaHei', sans-serif;
  box-sizing: border-box;
}

.page-header {
  margin-bottom: 32px;
  text-align: center;
}

.page-header h2 {
  margin: 0;
  color: #2c3e50;
  font-size: 28px;
  font-weight: 600;
  letter-spacing: 1px;
}

.filter-card {
  margin-bottom: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  border: 1px solid #e4e7ed;
  background: #ffffff;
}

.filter-card :deep(.el-card__body) {
  padding: 20px;
}

.filter-form {
  margin-bottom: 0;
}

.filter-form :deep(.el-form-item) {
  margin-bottom: 16px;
  margin-right: 24px;
}

.filter-form :deep(.el-form-item__label) {
  font-weight: 600;
  color: #2c3e50;
  font-size: 14px;
}

.feedback-card {
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  border: 1px solid #e4e7ed;
  background: #ffffff;
  overflow: hidden;
}

.feedback-card :deep(.el-card__header) {
  background: #f5f7fa;
  border-bottom: 1px solid #ebeef5;
  padding: 16px 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.card-header span {
  color: #2c3e50;
  font-weight: 600;
  font-size: 16px;
}

.header-actions .el-button {
  border-radius: 4px;
  font-weight: 500;
  padding: 8px 16px;
  transition: all 0.3s ease;
}

.feedback-content {
  padding: 0;
}

.feedback-content :deep(.el-table) {
  border-radius: 0;
}

.feedback-content :deep(.el-table__header) {
  background: #f5f7fa;
}

.feedback-content :deep(.el-table th) {
  background: transparent;
  color: #2c3e50;
  font-weight: 600;
  font-size: 14px;
  border-bottom: 1px solid #ebeef5;
  padding: 12px;
}

.feedback-content :deep(.el-table td) {
  padding: 12px;
  border-bottom: 1px solid #ebeef5;
  color: #606266;
  font-size: 14px;
}

.feedback-content :deep(.el-table__row:hover) {
  background: #f5f7fa;
}

.feedback-content :deep(.el-table__row:nth-child(even)) {
  background: #fafafa;
}

.feedback-content :deep(.el-table__row:nth-child(even):hover) {
  background: #f5f7fa;
}

.feedback-content :deep(.el-button) {
  border-radius: 4px;
  font-weight: 500;
  padding: 6px 12px;
  font-size: 12px;
  transition: all 0.3s ease;
}

.pagination-container {
  margin-top: 16px;
  padding: 16px 20px;
  display: flex;
  justify-content: center;
  background: #f5f7fa;
  border-top: 1px solid #ebeef5;
}

.pagination-container :deep(.el-pagination) {
  --el-pagination-bg-color: transparent;
  --el-pagination-text-color: #606266;
  --el-pagination-border-radius: 4px;
}

/* 反馈详情样式 */
.feedback-detail {
  padding: 20px 0;
}

.feedback-detail :deep(.el-descriptions) {
  border-radius: 8px;
  overflow: hidden;
  margin-bottom: 20px;
}

.feedback-detail :deep(.el-descriptions__header) {
  background: #f5f7fa;
  padding: 16px 20px;
  border-bottom: 1px solid #ebeef5;
}

.feedback-detail :deep(.el-descriptions__title) {
  color: #2c3e50;
  font-weight: 600;
  font-size: 16px;
}

.feedback-detail :deep(.el-descriptions__body) {
  background: #ffffff;
}

.feedback-detail :deep(.el-descriptions-item__label) {
  background: #fafafa;
  color: #606266;
  font-weight: 600;
  width: 120px;
}

.feedback-detail :deep(.el-descriptions-item__content) {
  color: #2c3e50;
  padding: 12px 20px;
}

.content-text {
  line-height: 1.6;
  white-space: pre-wrap;
  word-break: break-word;
}

.reply-section {
  margin-top: 20px;
}

.reply-section h4 {
  color: #2c3e50;
  font-weight: 600;
  margin-bottom: 16px;
  padding-bottom: 8px;
  border-bottom: 2px solid #409eff;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
  padding-top: 16px;
}

.dialog-footer .el-button {
  border-radius: 4px;
  font-weight: 500;
  padding: 10px 20px;
  transition: all 0.3s ease;
}

.dialog-footer .el-button--primary {
  background: #409eff;
  border-color: #409eff;
}

.dialog-footer .el-button--primary:hover {
  background: #66b1ff;
  border-color: #66b1ff;
}

/* 对话框样式优化 */
:deep(.el-dialog) {
  border-radius: 8px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
  border: 1px solid #e4e7ed;
}

:deep(.el-dialog__header) {
  background: #f5f7fa;
  border-radius: 8px 8px 0 0;
  padding: 16px 20px;
  border-bottom: 1px solid #ebeef5;
}

:deep(.el-dialog__title) {
  color: #2c3e50;
  font-weight: 600;
  font-size: 16px;
}

:deep(.el-dialog__body) {
  padding: 20px;
}

:deep(.el-dialog__footer) {
  padding: 16px 20px;
  background: #f5f7fa;
  border-radius: 0 0 8px 8px;
  border-top: 1px solid #ebeef5;
}

/* 表单样式优化 */
:deep(.el-form-item__label) {
  color: #2c3e50;
  font-weight: 600;
  font-size: 14px;
}

:deep(.el-input__wrapper) {
  border-radius: 4px;
  border: 1px solid #dcdfe6;
  transition: all 0.3s ease;
}

:deep(.el-input__wrapper:hover) {
  border-color: #409eff;
}

:deep(.el-input__wrapper.is-focus) {
  border-color: #409eff;
  box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.2);
}

:deep(.el-select .el-input__wrapper) {
  border-radius: 4px;
  border: 1px solid #dcdfe6;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .teacher-feedback {
    padding: 16px;
  }
  
  .page-header h2 {
    font-size: 24px;
  }
  
  .filter-form :deep(.el-form-item) {
    margin-right: 0;
    width: 100%;
  }
  
  .filter-form :deep(.el-input),
  .filter-form :deep(.el-select) {
    width: 100%;
  }
  
  .pagination-container {
    padding: 16px;
  }
}

@media (max-width: 480px) {
  .teacher-feedback {
    padding: 12px;
  }
  
  .page-header h2 {
    font-size: 20px;
  }
  
  .filter-card :deep(.el-card__body) {
    padding: 16px;
  }
  
  .feedback-content :deep(.el-table th),
  .feedback-content :deep(.el-table td) {
    padding: 12px 8px;
    font-size: 13px;
  }
}
</style>


